package com.leyou.user.service.service.impl;

import com.leyou.auth.inter.pojo.UserInfo;
import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.common.util.IdWorker;
import com.leyou.user.inter.dto.UserAdressUpdateDTO;
import com.leyou.user.inter.pojo.Area;
import com.leyou.user.inter.pojo.UserAdress;
import com.leyou.user.service.config.IdWorkerProperties;
import com.leyou.user.service.interceptor.LoginInterceptor;
import com.leyou.user.service.mapper.AreaMapper;
import com.leyou.user.service.mapper.UserAdressMapper;
import com.leyou.user.service.service.AdressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zqq
 * @ClassName AdressServiceImpl
 * @Description
 * @date 2020/4/5-14:16
 */
@Slf4j
@Service
public class AdressServiceImpl implements AdressService {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private UserAdressMapper userAdressMapper;

    @Autowired
    private IdWorkerProperties idWorkerProperties;

    @Override
    public Result<List<Area>> queryAreaByParentId(Integer parentId) {
        Area area = new Area();
        area.setParentId(parentId);
        List<Area> areas = null;
        try {
            areas = this.areaMapper.select(area);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(areas);
    }

    @Override
    public Result<List<UserAdress>> queryUserAdressByAll() {
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        UserAdress userAdress = new UserAdress();
        userAdress.setUserId(loginUser.getId());
        List<UserAdress> userAdresses = null;
        try {
            userAdresses = this.userAdressMapper.select(userAdress);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(userAdresses);
    }

    @Override
    public Result<UserAdress> queryUserAdressById(Long id) {
        UserAdress userAdress = null;
        try {
            userAdress = this.userAdressMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(userAdress);
    }

    @Override
    public Result<UserAdress> queryUserAdressByDefault() {
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        UserAdress dto = new UserAdress();
        dto.setUserId(loginUser.getId());
        dto.setIsFlag(true);
        UserAdress userAdress = null;
        try {
            userAdress = this.userAdressMapper.selectOne(dto);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(userAdress);
    }

    @Override
    public Result addUserAdress(UserAdressUpdateDTO dto) {
        IdWorker idWorker = new IdWorker(idWorkerProperties.getWorkerId(),idWorkerProperties.getDatacenterId());
        UserInfo loginUser = LoginInterceptor.getLoginUser();
        UserAdress userAdress = new UserAdress();
        BeanUtils.copyProperties(dto, userAdress);
        userAdress.setId(idWorker.nextId());
        userAdress.setUserId(loginUser.getId());
        //设置默认选中
        Result<UserAdress> userAdressResult = this.queryUserAdressByDefault();
        if(!userAdressResult.success())
           return userAdressResult;
        if(userAdressResult.getData() == null){
            userAdress.setIsFlag(true);
        }else{
            userAdress.setIsFlag(false);
        }
        try {
            this.userAdressMapper.insertSelective(userAdress);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result updateUserAdress(UserAdressUpdateDTO dto) {
        if(dto == null || dto.getId() == null)
            return Result.newFailure(ResultCode.PARAM_NOT_NULL);
        UserAdress userAdress = new UserAdress();
        BeanUtils.copyProperties(dto, userAdress);
        try {
            this.userAdressMapper.updateByPrimaryKeySelective(userAdress);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result deleteUserAdress(Long id) {
        try {
            this.userAdressMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    @Transactional
    public Result updateDefaultAdress(Long id) {
        UserInfo userInfo = LoginInterceptor.getLoginUser();
        UserAdress userAdress = new UserAdress();
        userAdress.setId(id);
        userAdress.setIsFlag(true);
        try {
            this.userAdressMapper.updateUserAdress(userInfo.getId());
            this.userAdressMapper.updateByPrimaryKeySelective(userAdress);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }
}
